const { SuccessModel, ErrorModel } = require("../../model/resModel");
const {getList, getListById, getDetail, createNewOrder, deleteOrder, changeOrder } = require('../../controllers/admin/order');

const handleOrderRoute = (req, res) => {
    const method = req.method;
    const id = req.query.id;
    const orderData = req.body;

    //新建订单
    if (method === 'POST' && req.path === '/api/order/new') {
        const newOrderDataPromise = createNewOrder(orderData);
        return newOrderDataPromise.then(newOrderData => {
            return new SuccessModel(newOrderData);
        })
        
    }

    //删除订单
    if (method === 'POST' && req.path === '/api/order/delete') {
        const deletedOrderPromise = deleteOrder(id);
        return deletedOrderPromise.then(deletedOrderData => {
            if (deletedOrderData) {
                return new SuccessModel(`删除订单${id}成功`);
            } else {
                return new ErrorModel('删除失败');
            }
        })
        
    }

    //更新订单状态
    if (method === 'POST' && req.path === '/api/order/change') {
        const changeOrderPromise = changeOrder(id, orderData);
        return changeOrderPromise.then(changeOrderData => {
            if (changeOrderData) {
                return new SuccessModel(`更新订单${id}成功`);
            } else {
                return new ErrorModel('更新订单失败');
            }
        })
    }

    //获取订单列表
    if (method === 'GET' && req.path === '/api/order/list') {
        const listDataPromise = getList();
        return listDataPromise.then(listData => {
            return new SuccessModel(listData,'订单列表');
        });
    }

    //前台获取订单列表
    if (method === 'POST' && req.path === '/api/order/idlist') {
        const listDataPromise = getListById(orderData);
        console.log('前段传进来的orderdata',orderData);
        return listDataPromise.then(listData => {
            return new SuccessModel(listData, '订单列表');
        });
    }

    //获取订单详情
    if (method === 'GET' && req.path === '/api/order/detail') {
        const detailDataPromise = getDetail(id);
        return detailDataPromise.then(detailData => {
            return new SuccessModel(detailData[0],`订单${id}的详情信息`);
        })
    }



}

module.exports = handleOrderRoute;